<% layout('/layouts/default.html', {title: '考核催办', libs: ['validate','fileupload','dataGrid']}){ %>
<div class="main-content">
  <div class="box box-main">
    <#form:form id="inputForm" model="${cuibanPage}" action="${ctx}/score/dcSupExamDXZBScore/cuibanDXCK" method="post" class="form-horizontal">
    <div class="box-body" style="height: 100px">
      <div class="row" style="height: 70px;">
        <div class="col-xs-12" style="margin-top: 20px;">
          <div class="form-group">
            <label class="control-label col-sm-2" title="">
              <span class="required">*</span> 短信内容：<i class="fa icon-question hide"></i></label>
            <div class="col-sm-10">
              <#form:textarea path="message" maxlength="100" rows="5" class="form-control required"/>
              <#form:hidden path="id"/>
            </div>
          </div>
         <div class="form-group">
            <label class="control-label col-sm-2" title="">
              <span class="required">*</span> 选择催办人：<i class="fa icon-question hide"></i></label>
            <div class="col-sm-10 table-form">
              <button type="button" class="btn btn-sm btn-primary" onclick="chooseUser()">选择催办人</button>
              <div style="display: none !important;">
                <#form:listselect id="auditor2" title="催办人"
                url="${ctx}/sys/empUser/customizedUserSelect?remarks=${orgCode}" allowClear="false"
                labelPath="name" path="userCode" callbackFuncName="listselectCallback"
                checkbox="true" itemCode="userCode" itemName="userName"
                placeholder="请选择催办人"/>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-xs-12">
              <div class="form-group">
                <label class="control-label col-sm-2">
                  <span class="required hide">*</span> </label>
                <div class="col-sm-10 table-form">
                  <table id="scheBusiTaskFeedbackDataGrid"></table>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="box-footer">
      <div class="row" style="text-align: right;">
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('确 定')}</button>&nbsp;
          <button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
        </div>
      </div>
    </div>
  </#form:form>
</div>
</div>
<% } %>
<script>
  $("#inputForm").validate({
    submitHandler: function(form){
      var dd = $("#scheBusiTaskFeedbackDataGrid").dataGrid('getRowData');
      if(dd.length==0){
        js.showMessage("请选择催办人")
        return;
      }
      $('#scheBusiTaskFeedbackDataGrid').dataGrid('updateListFieldName');
      js.ajaxSubmitForm($(form), function(data){
        js.showMessage(data.message);
        if(data.result == Global.TRUE){
          // location.reload();
          // $('#dataGrid').dataGrid('refresh', 1, 20);
          js.closeCurrentTabPage(function(contentWindow){
            contentWindow.page();
          });
        }
      }, "json");
    }
  });

  //初始化多次反馈表DataGrid对象
  $("#scheBusiTaskFeedbackDataGrid").dataGrid({
    data:${toJson(cuibanPage.userList)},
    datatype: "local", // 设置本地数据
    autoGridHeight: function(){return 'auto'}, // 设置自动高度
    // 设置数据表格列
    columnModel: [
      {header:'${text("id")}', name:'id',hidden:true, width:100, editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'}},
      {header:'${text("催办人编码")}', name:'userCode',hidden:true, width:100, editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'}},
      {header:'${text("催办人")}', name:'name', width:100, editable:true, edittype:'text', editoptions:{'maxlength':'200', 'class':'form-control'}},
      {header:'${text("催办人电话")}', name:'phone', width:100, editable:true, edittype:'text', editoptions:{'maxlength':'11', 'class':'form-control'}},

      {header:'${text("操作")}', name:'actions', width:80, align:'center', formatter: function(val, obj, row, act){
          var actions = [];
          actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){delOne(\''+obj.rowId+'\',\''+row.name+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
          return actions.join('');
        }, editoptions: {defaultValue: 'new'}}
    ],

    // 编辑表格参数
    editGrid: true,				// 是否是编辑表格
    editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
    emptyDataHint: false, 	// 表格内没有数据的时候是否提示 “无数据显示”  v4.1.7
    emptyDataHintContent: '<center>暂无数据</center>', // 没有数据的提示内容
    editGridAddRowBtn: $('#testDataChildDataGridAddRowBtn'),	// 子表增行按钮
    editGridAddRowBtnToHeader: false,	// 子表增行按钮是否显示到表头上
    editGridAddRowInitData: {id: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据
    // 编辑表格的提交数据参数
    editGridInputFormListName: 'userList', // 提交的数据列表名
    editGridInputFormListAttrs: 'userCode,name,phone,id', // 提交数据列表的属性字段
    // 加载成功后执行事件
    ajaxSuccess: function(data){

    }
  });

  $('#scheBusiTaskFeedbackDataGrid').dataGrid('setParam', {
    data:${toJson(defaultUserList)}
  }, true).dataGrid('reloadGrid');

  // 选择催办人
  function chooseUser(){
    // 打开隐藏的人员选择器
    $("#auditor2Button").click();
  }

  // 删除催办人
  function delOne(rowId,userName){
    // 在前端删除展示列表中的催办人
    $('#scheBusiTaskFeedbackDataGrid').dataGrid('delRowData',rowId);
    // 删除隐藏的人员选择器中的催办人
    let tempUserCode = $('#auditor2Code').val();
    let tempUserName = $('#auditor2Name').val();
    // 将编码和名称替换，需要注意分隔符的处理
    tempUserCode = tempUserCode.replace(','+rowId,'').replace(rowId+',','').replace(rowId,'');
    tempUserName = tempUserName.replace(','+userName,'').replace(userName+',','').replace(userName,'');
    $('#auditor2Code').val(tempUserCode);
    $('#auditor2Name').val(tempUserName);
  }

  // 选择催办人后，更新催办人列表
  function listselectCallback(id, act, index, layero, selectData){
      var body =  js.layer.getChildFrame('body', index);//取子页面对象
      var selectList = $(body).find("#selectDataList").val();

      $('#scheBusiTaskFeedbackDataGrid').dataGrid('setParam', {
        data:JSON.parse(selectList)
      }, true).dataGrid('reloadGrid');
  }

</script>